﻿namespace MSBuild.MKS.Tasks
{
    using System;

    using Microsoft.Build.Framework;
    using Microsoft.Build.Utilities;

    /// <summary>
    /// The checkin.
    /// </summary>
    public class Checkin : Task
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets ChangePackageID.
        /// </summary>
        [Required]
        public string ChangePackageID { get; set; }

        /// <summary>
        /// Gets or sets CheckinComments.
        /// </summary>
        [Required]
        public string CheckinComments { get; set; }

        /// <summary>
        /// Gets or sets Files.
        /// </summary>
        [Required]
        public ITaskItem[] Files { get; set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// The execute.
        /// </summary>
        /// <returns>
        /// The execute.
        /// </returns>
        public override bool Execute()
        {
            try
            {
                MksCommands cmds = new MksCommands();
                cmds.ChangePackageID = this.ChangePackageID;
                cmds.CheckinComments = this.CheckinComments;
                cmds.Log = this.Log;

                foreach (var item in this.Files)
                {
                    cmds.Checkin(item.ItemSpec);
                }

                return true;
            }
            catch (Exception ex)
            {
                this.Log.LogError("Exception during {0} procedure: {1}", this.GetType().Name, ex.GetType().Name);
                this.Log.LogError("Exception details: {0}", ex.Message);
                return false;
            }
        }

        #endregion
    }
}